// #include <QGuiApplication>
// #include <QQmlApplicationEngine>

#include <QtWidgets/QApplication>
#include <QtQuick/QQuickView>
#include <QtCore/QDebug>
#include <QtCore/QDir>
#include <QtQml/QQmlContext>
#include <QtQml/QQmlEngine>
#include <QFont>
#include <QFontDatabase>
#include <QString>
#include <QStringList>

#include <thread>
#include <chrono>

#include "fontmanager.h"
#include "ctrlclass/ctrl_hw.h"
#include "hwclass/hw_motor_class.h"

static int run_thread_readTH = 1;
static HW_Motor_Class *motorclass = HW_Motor_Class::getHandler();

static void thread_readTH(){
    while(run_thread_readTH > 0)
    {
        motorclass->set_motor_speed(0);
        motorclass->running();
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
        qDebug("th_readTH thread exit finished");
    }
}

int main(int argc, char *argv[])
{
    // QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QCoreApplication::setAttribute(Qt::AA_DisableHighDpiScaling);
    QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);

    Ctrl_hw *ptr_hw = Ctrl_hw::getHandler();


    std::thread* th_readTH = new std::thread(thread_readTH);
    // std::thread* tvpnc2 = new std::thread(thread_readTH);
    // std::thread* tvpnc3 = new std::thread(thread_readTH);

    QApplication app(argc, argv);

    FontManager fontManager;
    QString fontFamily = fontManager.loadFont(":/wqy-microhei.ttc");
    if (fontFamily.isEmpty()) {
        qCritical() << "Failed to load font, fallback to system default.";
    } else {
        // 使用加载的字体家族名称设置全局字体
        if (!fontManager.setGlobalFont(fontFamily, 16)) {
            qCritical() << "Failed to set global font.";
        }
        else {
            qDebug("Set global font OK");
        }
    }

    QQuickView viewer;
    viewer.engine()->rootContext()->setContextProperty("ptr_hw", ptr_hw);
    // viewer.engine()->addImportPath(QApplication::applicationDirPath());
    // QObject::connect(viewer.engine(), &QQmlEngine::quit, &viewer, &QWindow::close);

    // viewer.setTitle(QStringLiteral("QML Charts"));
    viewer.setSource(QUrl("qrc:/main.qml"));
    // viewer.setResizeMode(QQuickView::SizeRootObjectToView);
    // viewer.setMinimumSize(QSize(1024,600));
    viewer.setGeometry(0,0,1024,600);
    viewer.show();

    app.exec();
    qDebug("GUI thread exit finished");
    run_thread_readTH = 0;
    th_readTH->join();
    qDebug("th_readTH thread exit finished");



    return 0;
}



    // QQuickView view;
    // view.engine()->rootContext()->setContextProperty("myObject", new MyClass()); // 注册对象到QML环境里。注意需要管理好内存！或者使用智能指针。
    // view.setSource(QUrl("qrc:/main.qml")); // 设置QML文件路径。确保已经添加到资源文件。
    // view.show(); // 显示视图。
    // return app.exec(); // 运行应用程序事件循环。记得释放对象！例如使用QObject::deleteLater()。 示例中未展示。 实际应用时需要确保对象生命周期管理正确。例如在合适的时候delete指针指向的对象。或者使用智能指针避免裸指针管理。这里仅为展示如何设置属性，实际应用中还需考虑内存管理等最佳实践。例如：QObject::deleteLater() 或者 使用 QSharedPointer/QScopedPointer 等智能指针管理对象生命周期。此处仅为演示如何在 QQuickView 中设置属性，实际应用还需考虑内存管理等最佳实践。例如：QObject::deleteLater() 或者 使用 QSharedPointer/QScopedPointer 等智能指针管理对象生命周期。此处仅为



